﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.IO;

namespace 作业11
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        public byte[] GetFileBytes(string Filename)
        {
            if (Filename=="")
            return null;
            try
            {
                FileStream fileStream = new FileStream(Filename, FileMode.Open, FileAccess.Read);
                BinaryReader binaryReader = new BinaryReader(fileStream);
                byte[] fileBytes = binaryReader.ReadBytes((int)fileStream.Length);
                binaryReader.Close();
                fileStream.Close();
                return fileBytes;
            }
            catch
            {
                return null;
            
            }

        }
        public bool WriteFileBytes(string TargetFilename, byte[] fileBytes)
        {
            bool k = true;
            if (TargetFilename != "" && fileBytes.Length != 0)
            {
                try
                {
                    FileStream fileStream = new FileStream(TargetFilename, FileMode.OpenOrCreate, FileAccess.Write);
                    BinaryWriter binaryWrite = new BinaryWriter(fileStream);
                    binaryWrite.Write(fileBytes);
                    binaryWrite.Flush();
                    binaryWrite.Close();
                    fileStream.Close();

                }
                catch
                {
                    k = false;

                }
            }
            else 
            { 
                k = false; 
            }
            return k;
        }
        private void menuItem2_Click(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog openfile = new OpenFileDialog();
                openfile.Filter = "jpg类型图片(*.jpg)|*.jpg|BMP类型图片(*.BMP)|*.BMP";
                if (openfile.ShowDialog() == DialogResult.OK)
                {
                    byte[] picbinary = GetFileBytes(openfile.FileName);
                    MemoryStream mempicstream = new MemoryStream(picbinary);
                    pictureBox1.Image = Image.FromStream(mempicstream);
                    mempicstream.Dispose();
                    mempicstream.Close();
                }
            }
            catch (Exception m)
            {
                MessageBox.Show("读取图片出差，可能的问题是:" + Convert.ToString(m), "错误提示");
            }
        }

        private void menuItem4_Click(object sender, EventArgs e)
        {
            try
            {
                if (pictureBox1.Image == null)
                {
                    MessageBox.Show("禁止图片为空的时候另存信息。", "错误提示");

                }
                else
                {
                   saveFileDialog1.Filter = "jpg类型图片(*.jpg)|*.jpg";
                   DialogResult result = saveFileDialog1.ShowDialog();
                   if (result == DialogResult.OK)
                   {
                       MemoryStream ms = new MemoryStream();
                       Bitmap bm = new Bitmap(pictureBox1.Image);
                       bm.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
                       byte[] bytes = ms.ToArray();
                       WriteFileBytes(saveFileDialog1.FileName, bytes);
                       MessageBox.Show("另存图片成功", "提示");
                       ms.Dispose();
                       ms.Close();
                       bm.Dispose();
                  }
                 }}
            catch (Exception m)
            {
                MessageBox.Show("读取图片出差，可能的问题是:" + Convert.ToString(m), "错误提示");
            }
                       
                   
                }

        private void menuItem1_Click(object sender, EventArgs e)
        {

        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }
            }
        }
    
